﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HZ.DrugTrade.Web.Enterprise.RelationQuery
{
    public partial class DeliverySelectAreaManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSelectArea();
            }
        }
        public void BindSelectArea()
        {
            string projectid = "";
            switch (ddlProjectid.SelectedValue)
            {  
                case "0001":
                    projectid = " and ishcygcg is null";
                    break;
                case "0002":
                    projectid = " and ishcygcg = 1";
                    break;
                default:
                    break;
            }
            string userid = CurrentAccount.GetUser().UserID;
            this.gvwArea.DataSource = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().GetSelectArea(userid, projectid);
            this.gvwArea.DataBind();
            for (int i = 0; i < this.gvwArea.Rows.Count; i++)
            {
                CheckBox ckb = ((CheckBox)gvwArea.Rows[i].FindControl("checkbox1"));
                LinkButton chk = (LinkButton)gvwArea.Rows[i].FindControl("lblCheck");
                if (chk.Text=="审核通过")
                {
                    ckb.Enabled = false;
                }
            }
        }
        //提交
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //选择的地区的Id
            string areaIds = "";
            //遍历列表的所有行
            for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
            {
                CheckBox ckb = ((CheckBox)gvwArea.Rows[_index].FindControl("checkbox1"));
                if (ckb.Checked)
                {
                    areaIds += this.gvwArea.DataKeys[_index].Value.ToString() + ",";
                }
            }
            if (areaIds!="")
            {
                bool flag = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().DealSelectArea(areaIds.Substring(0, areaIds.Length - 1), 1,CurrentAccount.GetUser().UserID);
                if (flag)
                {
                    this.Controls.Add(new LiteralControl("<script>alert('提交成功！');</script>"));
                    BindSelectArea();
                }
                else
                {
                    this.Controls.Add(new LiteralControl("<script>alert('提交失败或该配送区域已提交！');</script>"));
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("请至少选择一项", this);
            }
        }
        //删除
        protected void btnDel_Click(object sender, EventArgs e)
        {
            //选择的地区的Id
            string areaIds = "";
            //遍历列表的所有行
            for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
            {
                CheckBox ckb = ((CheckBox)gvwArea.Rows[_index].FindControl("checkbox1"));
                if (ckb.Checked)
                {
                    areaIds += this.gvwArea.DataKeys[_index].Value.ToString() + ",";
                }
            }
            if (areaIds != "")
            {
                bool flag = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().DealSelectArea(areaIds.Substring(0, areaIds.Length - 1), 0,CurrentAccount.GetUser().UserID);
                if (flag)
                {
                    this.Controls.Add(new LiteralControl("<script>alert('删除成功');</script>"));
                    BindSelectArea();
                }
                else
                {
                    this.Controls.Add(new LiteralControl("<script>alert('删除失败');</script>"));
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("请至少选择一项", this);
            }
        }

        protected void gvwArea_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if(e.CommandName == "Add")
            {
                bool flag = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().DealSelectArea(e.CommandArgument.ToString(), 1, CurrentAccount.GetUser().UserID);
                if (flag)
                {
                    this.Controls.Add(new LiteralControl("<script>alert('提交成功！');</script>"));
                    BindSelectArea();
                }
                else
                {
                    this.Controls.Add(new LiteralControl("<script>alert('提交失败或该配送区域已提交！');</script>"));
                }
            }
        }

        protected void gvwArea_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lb = (LinkButton)e.Row.FindControl("btnSubmit");
                Label lbl = (Label)e.Row.FindControl("Label1");

                if (CurrentAccount.GetUser().UserID.Substring(0, 2).ToUpper().Equals("QP") && Convert.ToInt32(CurrentAccount.GetUser().UserID.Substring(2, 4)) >= 435 && lbl.Text.Trim() == "未提交" || CurrentAccount.GetUser().UserID.ToUpper().Equals("QP0374"))
                {
                    lb.Enabled = true;
                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindSelectArea();
        }
         
    }
}
